﻿using System.Collections;
using System.Collections.Generic;

namespace Sidvall.Data
{
    public abstract class FetchCriteriaBase<TField, TRelation> : SortCriteriaBase<TField>, IRelationItemsProvider
    {
        #region Public Members

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public RelationCollection<TRelation> IncludedEntities { get; set; }

        #region Clean

        public virtual void Clean(IEnumerable<string> validParameterKeys, IEnumerable<TField> validFilterFieldIds, IEnumerable<TField> validSortFieldIds, bool cleanPaging,
            IEnumerable<TRelation> validRelations)
        {
            Clean(validParameterKeys, validFilterFieldIds, validSortFieldIds, cleanPaging);
            if (this.IncludedEntities != null)
                this.IncludedEntities.RemoveAllExcept(validRelations);
        }
        public virtual void Clean(IEnumerable<string> validParameterKeys, IEnumerable<string> validFilterFieldNames, IEnumerable<string> validSortFieldNames, bool cleanPaging,
            IEnumerable<string> validRelationNames)
        {
            Clean(validParameterKeys, validFilterFieldNames, validSortFieldNames, cleanPaging);
            if (this.IncludedEntities != null)
                this.IncludedEntities.RemoveAllExcept(validRelationNames);
        }

        #endregion
        #region Clear

        public override void Clear()
        {
            base.Clear();
            this.IncludedEntities = null;
        }

        #endregion

        #region IRelationItemsProvider Members

        IList IRelationItemsProvider.RelationItems
        {
            get
            {
                return this.IncludedEntities;
            }
        }

        #endregion

        #endregion
    }
}
